package com.baizhi.service.impl;

import com.baizhi.dao.CmfzAdminDao;
import com.baizhi.entity.CmfzAdmin;
import com.baizhi.service.CmfzAdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author: renpu
 * @Date: 2019/2/26 20:17
 * @Description:
 */
@Service
@Transactional
public class CmfzAdminServiceImpl implements CmfzAdminService {
private Logger loggerFactory=LoggerFactory.getLogger(CmfzAdminServiceImpl.class);

    @Autowired
    private CmfzAdminDao adminDao;

    @Override
    public Map selectOneT(String userName,String passWord) {
        Map map=new HashMap();

        //将用户输入的用户名称，以及密码封装到shiro框架提供的UsernamePasswordToken对象里，token类似于令牌；
        UsernamePasswordToken token=new UsernamePasswordToken(userName,passWord);

        //由SecurityUtils工具类调用相应的方法，来获取主体Subject对象；
        Subject subject = SecurityUtils.getSubject();
        try {

            //调用主体相应的登入方法；
            subject.login(token);
            map.put("mesg",true);
        }catch (UnknownAccountException e){
            map.put("mesg",false);
            loggerFactory.debug("账户输入有误");

        }catch (IncorrectCredentialsException e2){
            map.put("mesg",false);
            loggerFactory.debug("密码输入有误");
        }
        return map;
    }

    @Override
    public CmfzAdmin selectOne(CmfzAdmin cmfzAdmin) {
        return adminDao.selectOne(cmfzAdmin);
    }

    @Override
    public void updatePassword(CmfzAdmin admin) {
        adminDao.updatePassword(admin);
    }

    @Override
    public void insert(CmfzAdmin admin) {
        adminDao.insert(admin);

    }

    @Override
    public void delete(int id) {
        adminDao.delete(id);
    }

    @Override
    public Map selectAdmin(int rows, int page) {
        Map map=new HashMap();

        int count=adminDao.selectCount();
        int start=(page-1)*rows;
        int pagesize=rows;
        List<CmfzAdmin> list=adminDao.selectAdmin(start,pagesize);

        map.put("rows",list);
        map.put("total",count);
        return map;
    }
}
